package nju.software.xkxt.service.implement;

import java.util.ArrayList;
import java.util.List;

import nju.software.xkxt.data.dao.UserDao;
import nju.software.xkxt.data.dataobject.UserDO;
import nju.software.xkxt.service.UserService;
import nju.software.xkxt.service.convertor.UserConvertor;
import nju.software.xkxt.service.model.UserModel;

/**
 * @author typ
 * 
 */
public class UserServiceImpl implements UserService {
	UserDao userDao;

	public boolean addUser(UserModel userModel) {
		UserDO userDO = new UserDO();
		userDO = UserConvertor.modelToDo(userModel);
		int userId = userDao.getMax();
		userId++;
		userDO.setUserId(userId);
		boolean b = userDao.addUser(userDO);
		return b;
	}

	public boolean vertifyUser(String sch_num, String passwd) {
		boolean b = userDao.vertifyUser(sch_num, passwd);
		return b;
	}

	public UserModel getUserBySchoolNo(String sch_no) {
		UserDO userDO = userDao.getUserBySchoolNo(sch_no);
		UserModel userModel = new UserModel(userDO);
		return userModel;
	}

	public boolean vertifyPwd(String passwd) {
		boolean b = userDao.vertifyPwd(passwd);
		return b;
	}

	public boolean alterPwd(String new_pwd) {
		boolean b = userDao.alterPwd(new_pwd);
		return b;
	}

	public List<UserModel> getUserByRole(int role) {
		List<UserModel> userModels = new ArrayList<UserModel>();
		List<UserDO> userDOs = userDao.getUserByRole(role);
		for (UserDO userDO : userDOs) {
			UserModel userModel = new UserModel(userDO);
			userModels.add(userModel);
		}
		return userModels;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public boolean deleteById(int userId) {
		boolean b = userDao.deleteById(userId);
		return b;
	}

	@Override
	public UserModel getUserById(int userId) {
		UserDO userDO = userDao.getUserById(userId);
		UserModel userModel = new UserModel(userDO);
		return userModel;
	}
}
